var http = require('https');
var schedule = require("node-schedule");  // 定时 插件 可以整点去拿数据
var iconv = require("iconv-lite");
var cp = require('child_process')
var path = require('path')
var fs = require('fs')

var num = 10   // 定义需要提醒的长龙；

function httpGet(uri,name,num) {
  http.get(uri, function (res) {
    var datas = [];
    var size = 0;
    console.log("开始请求数据");
    
    res.on('data', function (data) {
      datas.push(data);
      size += data.length;
      //process.stdout.write(data);  
    });

    res.on("end", function () {
      var buff = Buffer.concat(datas, size);
      var result = iconv.decode(buff, "utf8");//转码//var result = buff.toString();//不需要转编码,直接tostring  
      // console.log(result);
      var josn = JSON.parse(result)
      // console.log(json);

      josn.result.data.forEach(element => {
        if (element.count >= num) {
          initApp(element,name)
        } else {
          var state = getName(element.state)
          
          console.log(`${name}：第${element.rank}名：` + state +element.count + '期');
          
        }
      });
      console.log("++++++++++ 华丽的分割线 +++++++");
      
    })
  }).on('error', function (e) {
    console.log("  请求失败: " + e.message);
  });
}


 function getName(params) {
   var state = ""
  switch (params) {
    case 1:
      state = "单"
      break;
    case 2:
      state = "双"
      break;
    case 3:
      state = "大"
      break;
    case 4:
      state = "小"
      break;
    case 5:
      state = "龙"
      break;
    case 6:
      state = "虎"
      break;
    default:
      state = "未知"
      break;
  }
  return state;
 }

var initApp = function (element,name) {
  // var vbsPath = path.join(__dirname, 'vb.message.vbs')

  // cp.exec('cscript.exe ' + vbsPath + ' "提示" "此文件没有跟 OnceDoc 绑定"', function(err, stdout, stderr) {
  //   if (err) {
  //     fs.writeFileSync('log.log', err.toString())
  //   }
  // })
  // 使用ActiveXObject弹出消息框
  var  state = getName(element.state);
  cp.exec(`mshta "javascript:var sh=new ActiveXObject("WScript.Shell"); sh.Popup("${name}:第${element.rank}名：+++${state}+++ ${element.count}期了", 10, "赶紧的长龙来了!", 64 );close()"`);
}


//下面可以封装  但是 我就只需要 这两个 玩法 就够了。
var rule1 = new schedule.RecurrenceRule();
var times1 = [3, 8, 13, 18, 23, 28, 33, 38, 43, 48, 53, 58];
rule1.minute = times1;
schedule.scheduleJob(rule1, function () {
  var uri = `https://api.api68.com/pks/getPksLongDragonCount.do?date=&lotCode=10001`;
  httpGet(uri,"pk10",10);
}); 


var rule2 = new schedule.RecurrenceRule();
var times2 = [11,21,31,41,51];
rule2.minute = times2;
schedule.scheduleJob(rule2, function () {
  var uri = `https://api.api68.com/CQShiCai/getShiCaiDailyDragonCount.do?lotCode=10002`;
  httpGet(uri,"重庆时时彩",10);
}); 

